﻿using MailKit.Net.Smtp;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MimeKit;
using MimeKit.Text;
using System.Collections.Generic;

namespace VOL.WebApi.Controllers.MyTable.customer_api
{
    [Route("api/sendmail")]
    [ApiController]
    public class sendmailController : ControllerBase
    {
        [HttpPost, Route("send_mail")]
        public void send_mail()
        {
            var host = "smtp.139.com";
            var port = 465;
            var useSsl = true;
            //var from_username = "WLMmanage@ctruck.com.cn";
            var from_username = "15156307892@139.com";
            var from_password = "ef120f4c4899d0362000";//授权码，需要到139上获取
            var from_name = "测试";
            var from_address = "15156307892@139.com";

            var address = new List<MailboxAddress>
            {
                new MailboxAddress("111","502377846@qq.com"),
                new MailboxAddress("222","xianshun.huang@ctruck.com.cn")
            };
            MimeMessage message = new MimeMessage();
            message.From.Add(new MailboxAddress(from_name, from_address));
            message.To.AddRange(address);
            message.Subject = "关于系统上线的通知";
            var multipart = new Multipart("mixed");
            var plain = new TextPart(TextFormat.Plain);
            plain.Text= "关于系统上线的通知正文关于系统上线的通知正文关于系统上线的通知正文关于系统上线的通知正文关于系统上线的通知正文关于系统上线的通知正文";
            multipart.Add(plain);
            message.Body = multipart;

            using var client = new SmtpClient
            {
                ServerCertificateValidationCallback = (s, c, h, e) => true
            };
            client.AuthenticationMechanisms.Remove("XOAUTH2");

            client.Connect(host, port, useSsl);
            client.Authenticate(from_username, from_password);
            client.Send(message);
            client.Disconnect(true);
        }
    }
}
